﻿using System;
using System.Web;
using Models;

namespace BLL
{
    public class WebWorkContext
    {
        private const string CustomerCookieName = "CSMF.user";
        private AuthenticationService _authenticationService;
        private UserService _userService;
        private UserInfo _cachedCustomer;
        public WebWorkContext()
        {
            _authenticationService = new AuthenticationService();
            _userService = new UserService();
        }


        public UserInfo CurrentUser
        {
            get
            {
                return GetCurrentUser();
            }
            set
            {
                SetCustomerCookie(value.UserGuid);
                _cachedCustomer = value;
            }
        }

        protected UserInfo GetCurrentUser()
        {

            if (_cachedCustomer != null)
                return _cachedCustomer;

            UserInfo customer = null;
            if (HttpContext.Current != null)
            {

                //registered user
                if (customer == null || customer.Deleted || !customer.Active)
                {
                    customer = _authenticationService.GetAuthenticatedCustomer();
                }

              

                //load guest customer
                if (customer == null || customer.Deleted || !customer.Active)
                {
                    var customerCookie = GetCustomerCookie();
                    if (customerCookie != null && !String.IsNullOrEmpty(customerCookie.Value))
                    {
                        Guid customerGuid = new Guid(customerCookie.Value);

                        var customerByCookie = _userService.GetUserByGuid(customerGuid);
                        if (customerByCookie != null &&
                            //this customer (from cookie) should not be registered
                            !customerByCookie.IsRegistered() )
                            customer = customerByCookie;
                    }
                }

                //create guest if not exists
                if (customer == null || customer.Deleted || !customer.Active)
                {
                    customer = _userService.InsertGuestCustomer();
                }

                SetCustomerCookie(customer.UserGuid);
            }


            _cachedCustomer = customer;
            return customer;
        }

        private void SetCustomerCookie(Guid userGuid)
        {
            var cookie = new HttpCookie(CustomerCookieName);
            cookie.Value = userGuid.ToString();
            if (userGuid== Guid.Empty)
            {
                cookie.Expires = DateTime.Now.AddMonths(-1);
            }
            else
            {
                int cookieExpires = 24 * 365; //TODO make configurable
                cookie.Expires = DateTime.Now.AddHours(cookieExpires);
            }
            var _httpContext = HttpContext.Current;

            if (_httpContext != null && _httpContext.Response != null)
            {
                _httpContext.Response.Cookies.Remove(CustomerCookieName);
                _httpContext.Response.Cookies.Add(cookie);
            }
        }

        private HttpCookie GetCustomerCookie()
        {
            if (HttpContext.Current == null || HttpContext.Current.Request == null)
                return null;

            return HttpContext.Current.Request.Cookies[CustomerCookieName];
        }
    }
}